import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import { getUserByToken } from '../request/api'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login/Login.vue')
  },
]

const allRoutes = {
  '/good': {
    path: '/good',
    component: () => import(/* webpackChunkName: "good" */ '../views/good/Good.vue')
  },
  '/category': {
    path: '/category',
    component: () => import(/* webpackChunkName: "category" */ '../views/category/Category.vue'),
  },
  '/brand': {
    path: '/brand',
    component: () => import(/* webpackChunkName: "brand" */ '../views/brand/Brand.vue')
  },
  '/attribute': {
    path: '/attribute',
    component: () => import(/* webpackChunkName: "attribute" */ '../views/attribute/Attribute.vue')
  },
  '/order': {
    path: '/order',
    component: () => import(/* webpackChunkName: "order" */ '../views/order/Order.vue')
  },
  '/topic': {
    path: '/topic',
    component: () => import(/* webpackChunkName: "topic" */ '../views/topic/Topic.vue')
  },
  '/addgood': {
    path: '/addgood',
    component: () => import(/* webpackChunkName: "addgood" */ '../views/editgood/Addgood.vue')
  }, '/editgood': {
    path: '/editgood/:id',
    component: () => import(/* webpackChunkName: "editgood" */ '../views/editgood/Editgood.vue')
  },
  '/addcategory': {
    path: '/addcategory',
    component: () => import(/* webpackChunkName: "addcategory" */ '../views/category/addEditCategory/AddCategory.vue')
  },
  '/editcategory': {
    path: '/editcategory/:id',
    component: () => import(/* webpackChunkName: "editcategory" */ '../views/category/addEditCategory/EditCategory.vue')
  },
  '/addbrand': {
    path: '/addbrand',
    component: () => import(/* webpackChunkName: "addbrand" */ '../views/brand/AddBrand.vue')
  },
  '/editbrand': {
    path: '/editbrand/:id',
    component: () => import(/* webpackChunkName: "editbrand" */ '../views/brand/EditBrand.vue')
  },

}

console.log(store.state.userinfo)
// 根据不同的权限生成不同的路由路径

const newAllRoutesFn = () => {
  const newAllRoutes = []
  let url = ''
  for (var i = 0; i < store.state.userinfo.length; i++) {
    if (!url) {
      url = store.state.userinfo[0].path
    }
    if (allRoutes[store.state.userinfo[i].path]) {
      newAllRoutes.push(allRoutes[store.state.userinfo[i].path])
    }
  }
  // return { url, newAllRoutes }
  router.addRoutes([
    {
      path: '/',
      component: () => import(/* webpackChunkName: "home" */ '../views/home/Home.vue'),
      children: newAllRoutes
    }
  ])
  // console.log(newRoutes)
  return url
}
// console.log("newAllRoutes", newAllRoutesFn().newAllRoutes)



const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (localStorage.getItem('token')) {
    if (from.path === '/login' && to.path === '/home') {
      const url = newAllRoutesFn()
      // router.addRoutes([
      //   {
      //     path: '/',
      //     component: () => import(/* webpackChunkName: "home" */ '../views/home/Home.vue'),
      //     children: newRoutes.newAllRoutes
      //   }
      // ])
      // console.log(newRoutes)
      return next(url)
    } else if (store.state.userinfo.length == 0) {
      getUserByToken().then(res => {
        if (res.errno === 0) {
          store.commit("saveUserInfo", res.data.menu)
          const url = newAllRoutesFn()
          return next(url)
        }
      })
    } else if (to.path == '/login') {
      return next(from)
    }
    next()
  } else if (to.path !== '/login') {
    next('/login')
  } else { next() }
})

export default router
